package com.example.demo05.controller;


import com.example.demo05.pojo.Clock;
import com.example.demo05.pojo.ClockTable;
import com.example.demo05.service.ClockService;
import com.example.demo05.util.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author anxuan
 * @since 2021-06-19
 */
@RestController
@RequestMapping("/clock")
@Slf4j
public class ClockController {

    @Autowired
    ClockService clockService;

    // @Scheduled(cron = "0 0 0 1/1 * ? ")

    @Scheduled(cron = "0 0 0 * * ?")
    public void upflag(){
        clockService.upFlag();
        //System.out.println("lllllllllll");
    }


   //显示所有学生打卡信息

    @GetMapping("/all")
    @PreAuthorize("hasAuthority('管理员')")
    public List<ClockTable> selectAll(){

        List<ClockTable> list = clockService.selectAll();
        for(int i=0;i<list.size();i++){
            list.get(i).setClockTimeStr(DateTimeUtil.dateToStr(list.get(i).getClockTime(),"yyyy-MM-dd"));
        }
        return list;
    }

    //根据日期查找
    @GetMapping("/getForm")
    @PreAuthorize("hasAuthority('管理员')")
    public List<ClockTable> getForm(@RequestParam String date){

        log.info("进入");
        List<ClockTable> list = clockService.selectAll();
        List<ClockTable> orderlist = new ArrayList<>();

        for(int i=0;i<list.size();i++){
            list.get(i).setClockTimeStr(DateTimeUtil.dateToStr(list.get(i).getClockTime(),"yyyy-MM-dd"));
            if(Objects.equals(list.get(i).getClockTimeStr(), date)) {
                orderlist.add(list.get(i));
            }
        }
        return orderlist;
    }



    //根据日期查找异常
    @GetMapping("/exception")
    @PreAuthorize("hasAuthority('管理员')")
    public List<ClockTable> exception(@RequestParam String date){


        log.info("进入");
        List<ClockTable> list = clockService.selectAll();
        List<ClockTable> orderlist = new ArrayList<>();

        for(int i=0;i<list.size();i++){
            list.get(i).setClockTimeStr(DateTimeUtil.dateToStr(list.get(i).getClockTime(),"yyyy-MM-dd"));
            if(Objects.equals(list.get(i).getClockTimeStr(), date)&&list.get(i).getTemperature()>37.3) {
                orderlist.add(list.get(i));
            }
        }
        return orderlist;
    }







    //时间升序
    @GetMapping("/rise")
    public List<ClockTable> timeRise(){

        List<ClockTable> list = clockService.timeRise();
        for(int i=0;i<list.size();i++){
            list.get(i).setClockTimeStr(DateTimeUtil.dateToStr(list.get(i).getClockTime(),"yyyy-MM-dd HH:mm:ss"));
        }
        return list;

    }
    //时间降序
    @GetMapping("/drop")
    public List<ClockTable> timeDrop(){
        List<ClockTable> list = clockService.timeDrop();
        for(int i=0;i<list.size();i++){
            list.get(i).setClockTimeStr(DateTimeUtil.dateToStr(list.get(i).getClockTime(),"yyyy-MM-dd HH:mm:ss"));
        }
        return list;

    }
  //筛选温度大于37  所有天数
    @GetMapping("/tem")
    public List<ClockTable> selectTem(){
        return clockService.selectTem();
    }



    //打卡
    @PostMapping("/clockon")
    public void clockOn(@RequestParam double temperature,@RequestParam String loginid){

        log.info("温度,{}",temperature);
        log.info("学号,{}",loginid);
        Clock clock = new Clock();
        clock.setUserid(loginid);
        clock.setTemperature(temperature);
        log.info("clock,{}",clock);
        clockService.newClock(clock);

    }

    //根据日期查询
    @GetMapping("/test")
    public void test(@RequestParam String date){

        log.info("时间,{}",date);
       Date date1 = DateTimeUtil.strToDate(date,"yyyy-MM-dd");
       log.info("转化后时间,{}",date1);
    }





}

